<template>
	<view class="page">
		<view v-for="(item,index) in list" class="source-wrap" v-if="item.material_id==material_id">
			<view class="title fcb">
				<view class="fb f32">分享文案</view>
				<view class="copy-btn fcc" @click="copyText(item.dec)">复制文案</view>
			</view>
			<view class="msg-wrap mt10">
				<rich-text :nodes="item.dec"></rich-text>
			</view>
			
			<view class="title fcb mt20">
				<view class="fb f32">分享素材(已选{{choose_count}}/{{item.image.length}})</view>
			</view>
			
			<view class="image-wrap">
				<view v-for="(img,i) in item.image" class="image-box"  @click="chooseImg(index,i)">
					<radio :checked="img.is_choose" style="position: absolute;top: 0rpx;right: 0rpx;"/>
					<image :src="img.file_path"  ></image>
					<!-- @click.stop="preImg(item.image,i)" -->
				</view>
			</view>
			
		</view>
		
		
		<!-- <view class="source-wrap">
			<view class="source-item" v-for="item in list" v-if="item.material_id==material_id">
				<view class="user-box">
					<view class="user-msg">
						<image :src="item.avatar.file_path"></image>
						{{item.nickName}}
					</view>
					<view class="say-date">{{item.create_time}}</view>
				</view>
				<view class="content">
					{{item.material_title}}
				</view>
				<view>{{item.dec}}</view>
				<view class="image-wrap" v-if="item.material_type==10">
					<image :src="img.file_path" v-for="(img,i) in item.image"  @click.stop="preImg(item.image,i)"></image>
				</view>
				
				<view v-if="item.material_type==20">
					<video :src="item.video.file_path"></video>
				</view>
				<view class="handle-wrap">
					<view class="fcc" @click="copyText(item.material_title)">
						<image src="../../static/copy-icon.png"></image>
						复制文案
					</view>
					<view class="fcc" v-if="item.image.length!=0 || item.video.file_path!=''" @click="saveFile(item.image,item.video.file_path)">
						<image src="../../static/save-icon.png"></image>
						一键保存
					</view>
				</view>
				
			</view>
			<view class="null_tis" v-if="list.length==0">
				暂无数据~
			</view>
		</view> -->
		
		
		<!-- <navigator hoverClass="none" openType="navigate" url="/pages/source/release">
			<image class="release-wrap" src="../../static/images/say-icon.png"></image>
		</navigator> -->
		
		
		<!-- <view class="release-wrap">
			<view class="release-box">
				<navigator hoverClass="none" openType="navigate" url="/pages/source/release?is_image=1">
					<view v-if="showChooseAddType" class="releaseI">发布图片</view>
				</navigator>
				<navigator hoverClass="none" openType="navigate" url="/pages/source/release?is_image=0">
					<view v-if="showChooseAddType" class="releaseV">发布视频</view>
				</navigator>
			</view>
		</view> -->
		
		<view class="down-btn-wrap fcc">
			<view class="down-btn fcc" @click="saveFile()">下载</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				category_id:'',
				cotegory:[],
				list:[],
				page: 1,
				show: 0,
				getmore: 1,
				showChooseAddType:false,
				material_id:'',
				choose_count:0
			}
		},
		methods: {
				chooseImg(index,i){
					this.list[index].image[i].is_choose = !this.list[index].image[i].is_choose
					if(this.list[index].image[i].is_choose){
						this.choose_count +=1
					}else{
						this.choose_count -=1
					}
					this.$forceUpdate()
				},
			
				chooseAdd(){
					this.showChooseAddType = !this.showChooseAddType
				},
				
				// 保存文件
				saveFile(){
					let list_index = ''
					this.list.forEach((item,index)=>{
						if(item.material_id== this.material_id){
							list_index = index
						}
					})
					
					// 保存图片
					let img = []
					let video = this.list[list_index].video
					
					if(this.list[list_index].image.length!=0){
						for(let item of this.list[list_index].image){
							if(item.is_choose){
								img.push(item)
							}
						}
					}
					
					
					if(img.length!=0){
						for(let i=0;i<img.length;i++){
							uni.showLoading({
								title:`正在保存图片${i+1}`
							})
							uni.downloadFile({
							    url: `${img[i].file_path}`, //仅为示例，并非真实的资源
							    success: (res) => {
									uni.saveImageToPhotosAlbum({
									    filePath: res.tempFilePath,
									    success: function (res) {
											uni.hideLoading()
											uni.showToast({
												title:`图${i+1}保存成功`
											})
									    },
										fail() {
											uni.hideLoading()
											uni.showToast({
												title:`图${i+1}保存失败`,
												icon:'none'
											})
										}
									});
								},
								complete(e) {
									console.log(e)
								}
							});
						}
					// 保存视频
					}else if(video){
						uni.showLoading({
							title:"正在保存视频",
							icon:"none"
						})
						uni.downloadFile({
						    url: `${video}`, //仅为示例，并非真实的资源
						    success: (res) => {
								console.log(res)
								uni.saveFile({
								  tempFilePath: res.tempFilePath,
								  success (res) {
									console.log(res)
									uni.hideLoading()
									uni.showToast({
										title:"保存视频成功"
									})
								  },
								  fail() {
									console.log(res)
								  	uni.hideLoading()
									uni.showToast({
										title:"保存视频失败",
										icon:"none"
									})
								  }
								})
							}
						});
					}else{
						this.$comm.showtoast('请选择图片')
					}
				},
			
			  /**
			   * 生命周期函数--监听页面加载
			   */
			  onLoad: function (options) {
				this.material_id = options.material_id
				this.category_id = options.category_id
				this.getList()
			  },
			  
			  /*分享当前页面*/
			  onShareAppMessage() {
			  	let self = this;
			  	return {
			  		title: '分享素材',
			  		path: `/pages/sourceCenter/sourceDetail?category_id=${this.category_id}&material_id=${this.material_id}`
			  	};
			  },
			  
			  getList(){
				  var _this = this;
				  _this.loading = true;
				  _this._get(
				  	'plus.material.material/index',
				  	{
						 category_id:_this.category_id?_this.category_id:'',
						 page:_this.page,
						 limit:100
				  	},
				  	function(res) {
				  		console.log(res)
						let list = res.data.list.data
						if(list.length!=0){
							for(let item of list){
								for(let img of item.image){
									img.is_choose = false
								}
							}
						}
						
						if(_this.page==1){
							_this.list = list
						}else{
							if(list.length==0){
								uni.showToast({
									title:"没有更多了",
									icon:"none"
								})
							}else{
								_this.list = [..._this.list,...list]
							}
						}
				  		uni.hideLoading();
				  	}
				  );
			  },
			  
			  selected: function (id) {
			    this.category_id = id
			    this.list = []
			    this.page = 1
			    this.getList();
			  },
			  // 预览图片
			  preImg(list,index){
				  let urls = []
				  for(let item of list){
					  urls.push(item.file_path)
				  }
				  
				 uni.previewImage({
				 	urls:urls,
					current:index
				 })
			  },
			  // 复制文案
			  copyText(e){
				 uni.setClipboardData({
				     data: e,
				     success: function () {
				         console.log('success');
				     }
				 })
			  },
		},
	}
</script>

<style lang="scss">
	page{
		background-color: rgb(245,245,245);
	}
	.source-wrap{
		width: 710rpx;
		margin: 20rpx;
		padding: 20rpx;
		border-radius: 14rpx;
		background-color: #FFF;
		box-sizing: border-box;
		.title{
			height: 90rpx;
			.copy-btn{
				padding: 10rpx 20rpx;
				border-radius: 50rpx;
				border: 1rpx solid rgb(105,105,105);
				color: rgb(105,105,105);
			}
		}
		.msg-wrap{
			border-radius: 12rpx;
			padding: 20rpx;
			background-color: rgb(246,246,246);
		}
		
		.image-wrap{
			display: flex;
			flex-wrap: wrap;
			align-items: center;
			.image-box{
				position: relative;
				image{
					width: 210rpx;
					height: 210rpx;
					border-radius: 8rpx;
				}
			}
		}
	}
	
	.down-btn-wrap{
		position: fixed;
		bottom: 60rpx;
		width: 750rpx;
		.down-btn{
			background-color: rgb(226,35,26);
			color: #FFF;
			font-size: 30rpx;
			width: 680rpx;
			border-radius: 90rpx;
			height: 80rpx;
		}
	}
	
	/* .source-wrap{
		padding-top: 100rpx;
	}
	.source-item{
		width:710rpx;
		border-radius:16rpx;
		margin: 20rpx;
		background-color: #ffffff;
		padding: 30rpx;
		box-sizing: border-box;
	}
	.source-item>.user-box{
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
	.source-item>.user-box>.user-msg{
		display: flex;
		align-items: center;
		font-size: 24rpx;
		color: #999999;
	}
	.source-item>.user-box>.user-msg>image{
		width: 48rpx;
		height: 48rpx;
		border-radius: 50%;
		margin-right: 16rpx;
	}
	.source-item>.user-box>.say-date{
		font-size: 22rpx;
		color: #999;
	}
	.source-item>.content{
		font-size: 26rpx;
		color: #666666;
		font-weight:400;
		line-height:42rpx;
		margin: 18rpx 0rpx;
	}
	.image-wrap{
		display: flex;
		flex-wrap: wrap;
	}
	.image-wrap>image{
		width: 212rpx;
		height: 212rpx;
		border-radius: 10rpx;
		margin-right: 6rpx;
		margin-top: 6rpx;
	}
	.image-wrap>image:nth-child(3n) {
		margin-right: 0rpx;
	}
	.handle-wrap{
		display: flex;
		justify-content: flex-end;
	}
	.source-item>.handle-wrap>view{
		font-size: 24rpx;
		color: #333333;
		margin: 30rpx 0rpx 0rpx 46rpx;
	}
	.source-item>.handle-wrap>view>image{
		width: 28rpx;
		height: 28rpx;
		margin-right: 8rpx;
	}
	.release-wrap{
		position: fixed;
		right: 0;
		top: 750rpx;
		height: 118rpx;
		width: 130rpx;
	}
	.release-box{
		height: 100%;
		width: 100%;
		position: relative;
	}
	.release-box view{
		height: 60rpx;
		width: 60rpx;
		border-radius: 50%;
		position: absolute;
		background-color: rgb(200,20,43);
		color: #FFF;
		font-size: 22rpx;
		text-align: center;
		padding: 6rpx;
	}
	.releaseI{
		 top: -15rpx;
		 left: -10rpx;
	}
	.releaseV{
		bottom: -15rpx;
		left: -10rpx;
	}
	
	.cate-wrap{
		height:100rpx;
		width: 750rpx;
		position: fixed;
		top: 0;
		z-index: 1000;
		background-color: #FFFFFF;
		border-bottom: 1rpx solid #EEE;
		white-space: nowrap;
		padding-left: 10rpx;
		border-radius: 0rpx 0rpx 16rpx 16rpx;
		box-sizing: border-box;
	}
	.cate-item{
		height: 100%;
		padding: 0rpx 40rpx;
		display: inline-block;
		position: relative;
	}
	.cate-item>view{
		height: 100%;
		width: 100%;
	}
	.cate-item .line{
		position: absolute;
		bottom: 0rpx;
		width: 40rpx;
		height: 6rpx;
	}
	.line-active{
		background-color: #C30D23;
	} */
</style>
